var geoSelection = new Object();
geoSelection = {
  currentGeoChildsData : new Array(), //内部使用变量
  currentGeoData : new Array(),      //内部使用变量
  currentLevel : 1,
  backCurSelHandler: null,			//当前一个选择时回调function(level, selectobj)
  backLastSelHandler: null,			//最后一个选择时回调function(level, selectobj)
  backNullHandler: null, 			//下一个没有数据时回调function(level)

  selectionIdPre : "geo_level", //可配置变量#select框的ID名称
  includeGeoLayerPre : "geo_level", //可配置变量#包含select框的元素
  geoInputName : "geo_code",    //可配置变量#要使用的表单的geo_code名
  domainInputName : "domain",    //可配置变量#要使用的表单的domain名
  selectType : "selectType",  
  lastGeolevel : 5,             //可配置变量#geo层的最后一层是多少
  firstGeoLevel : 1,            //可配置变量#geo开始的级别
  geoLayerId : "geo_selection",
  txt: "-请选择-",  
  firstLevelCanChange : true,
  
  initGeoSelection : function (geoCode) {
//    try{
    this.currentGeoData[0] = 0;
    if(typeof(geoCode)=="undefined") geoCode = 0;
    var data = this.getTinJSON("index.php?"+TIN_CTL+"=ajaxgeo&"+TIN_ACT+"=getInitGeoTree&geo_code="+geoCode+"&start_level="+this.firstGeoLevel);
    for(var i = 0; i < data.length; i++) {
      if (data[i].child.length == 0) continue;
      var id = typeof data[i].current.ID == "undefined" ? 0 : data[i].current.ID;
      var parentId = data[i].child[0].PARENTID;
      var level = typeof data[i].current.GEOLEVEL == "undefined" ? this.firstGeoLevel+i : data[i].current.GEOLEVEL;

      if(data[i].child != null) {
        this.currentGeoChildsData[parentId] = data[i].child;
      }
      this.buildNewSelection(this.currentGeoChildsData[parentId], level, id);

      if(this.currentLevel < level && id > 0) {
        this.currentLevel = level;
      }
    }    

    for(var g = 1; g <= 5; g++) {
      if(g < this.firstGeoLevel || g > this.lastGeolevel) {
        $("#"+this.includeGeoLayerPre+g).hide();
      }
    }    
    //控制当前第一级是否可选
    if(typeof(geoCode) != "undefined" && geoCode > 0 && this.firstLevelCanChange == false) {
      $("#"+this.selectionIdPre+this.firstGeoLevel).attr("disabled", "disabled");
    }
//   }catch(ex){
//   }
    
    //初始化select框的动作属性
    $("#"+this.geoLayerId).find("select").each(function(level) {
      level+=1;


      $(this).change(function () {    	
        if(level < geoSelection.lastGeolevel) {
          for(var i = level+1; i <= geoSelection.lastGeolevel; i++) {
            $("#"+geoSelection.selectionIdPre+i).empty();
          }          
          //如果当前的select值不为空 则对其子选项初始化
          if($(this).val() != 0) {
            var data = geoSelection.getGeoChildByParentId($(this).val());
            geoSelection.buildNewSelection(data, level+1);
          }
        }

        geoSelection.currentLevel = 1;
        for(var i = geoSelection.firstGeoLevel; i <= geoSelection.lastGeolevel; i++) {
          if($("#"+geoSelection.selectionIdPre+i).val() > 0) {
            geoSelection.currentLevel = i;
          }
        }
        
        $("input[name="+geoSelection.geoInputName+"]").val(geoSelection.currentGeoData[$("#"+geoSelection.selectionIdPre+geoSelection.currentLevel).val()]);
        $("input[name="+geoSelection.domainInputName+"]").val($(this).find("option:selected").attr("id"));
        
        try {
          $("#set_category").get(0).disabled = false;
          $("#category_id").val(0);
          $("#category_id").attr("disabled", "disabled");
        } catch(e) {}
        if(geoSelection.backCurSelHandler!=null){geoSelection.backCurSelHandler(level,$(this));}
        //如果是最后设置的一个选择并且有设置回调，则调用
    	if(geoSelection.lastGeolevel==level && geoSelection.backLastSelHandler!=null){geoSelection.backLastSelHandler(level,$(this));}
      });
    });
    $("input[name="+this.geoInputName+"]").val(this.currentGeoData[$("#"+this.selectionIdPre+this.currentLevel).val()]);

},
  
  getGeoChildByParentId : function (parentId) {
    if(typeof(this.currentGeoChildsData[parentId]) != "undefined") {
      return this.currentGeoChildsData[parentId];
    } else {
      var data = this.getTinJSON("index.php?"+TIN_CTL+"=ajaxgeo&"+TIN_ACT+"=getGeoByParentId&parent_id="+parentId);
      //把取回来的数据缓存起来，防止重复取数据浪费资源
      this.currentGeoChildsData[parentId] = data;
      return this.currentGeoChildsData[parentId];
    }
  },
  initSelected : function (id) {    //zhangxilian 2012-3-9 根据id取geo_code
      var data = this.getTinJSON("index.php?"+TIN_CTL+"=ajaxgeo&"+TIN_ACT+"=GetGeoById&id="+id);
      geoSelection.initGeoSelection(""+data[0].GEO_CODE);    //68173824
  },
  checkSel : function () {
	  for(j=this.firstGeoLevel; j<=this.lastGeolevel; j++){
		var ob = $("#"+this.selectionIdPre+j);
  		if(ob.find("option").length>1){
  			if(ob.val() == null || ob.val()=="0"){
  				return false;
  			}
  		}
  	  }
	  return true;
  },
  getTinJSON : function(url) {
	  var result = null;
	  $.ajax({ type : "post", 
          url : url,
          async : false, 
          success : function(data){
            data = eval("(" + data + ")"); 
		  	result = data; 
          } 
          });
	     return result;
	   }
  ,
  buildNewSelection : function(data, level, selectedId) {
    var selectionObj = $("#"+this.selectionIdPre+level);
    selectionObj.empty();
    if(this.selectType == "select"){
        selectionObj.append("<option value='0'>-不限-</option>");
    }else{
        selectionObj.append("<option value='0'>-请选择-</option>");
    }
    if(data.length<=0){
    	for(j=level; j<=this.lastGeolevel; j++){
    		$("#"+this.selectionIdPre+j).hide();
    	}
    	if(this.backNullHandler!=null)this.backNullHandler(level);
    	return;
    }
    selectionObj.show();
    for(var i = 0; i < data.length; i ++) {
      if(data[i].ID == selectedId) {
        this.currentLevel = level;
        selectionObj.append("<option value='"+data[i].ID+"' selected='selected' id='"+data[i].ID+"'>"+data[i].DISPLAYNAME+"</option>");
      } else {
        selectionObj.append("<option value='"+data[i].ID+"' id='"+data[i].ID+"'>"+data[i].DISPLAYNAME+"</option>");
      }
      this.currentGeoData[data[i].ID] = data[i].GEO_CODE;
    }
  }
}
